<template>
  <q-dialog v-model="showDialog" persistent>
    <q-card>
      <q-card-section
        class="row items-center q-ml-lg q-mt-lg"
        style="min-width: 400px"
      >
        <q-avatar icon="warning_amber" color="red" text-color="white" />
        <span class="q-ml-md">部分信息错误，请下载错误信息查看</span>
      </q-card-section>

      <q-card-actions class="row justify-center q-pa-md">
        <div>
          <q-btn
            class="q-mr-sm"
            outline
            color="primary"
            icon="mdi-close-thick"
            label="关闭"
            v-close-popup
          />
          <q-btn
            class="q-ml-sm"
            color="primary"
            icon="mdi-cloud-download"
            label="下载"
            @click="downloadExcel(downloadPath)"
            v-close-popup
          />
        </div>
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
export default {
  name: 'confirm',
  props: {
    downloadPath: String,
  },
  data() {
    return {
      showDialog: false,
      exporting: false,
    };
  },
  methods: {
    show() {
      this.showDialog = true;
    },
    downloadExcel(path) {
      const str = path.split('/');
      const nameFile = str[str.length - 1];
      this.exporting = true;
      this.$downFile('dyn/file/pathFile', {
        pathFile: path,
      }).then((data) => {
        if (!data) {
          this.$message.warning('文件下载失败');
          return;
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), nameFile);
        } else {
          const url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }));
          const link = document.createElement('a');
          link.style.display = 'none';
          link.href = url;
          link.setAttribute('download', nameFile);
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link); // 下载完成移除元素
          window.URL.revokeObjectURL(url); // 释放掉blob对象
        }
      }).finally(() => {
        this.exporting = false;
      });
    },
  },
};
</script>
